//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class demo_2711 {
    public demo_2711() {
    }

    public static void main(String[] args) {
        int[][] grid = new int[][]{{1, 2, 3}, {3, 1, 5}, {3, 2, 1}};
        int[][] answer = differenceOfDistinctValues(grid);

        for(int[] ints : answer) {
            System.out.println(Arrays.toString(ints));
        }

    }

    public static int[][] differenceOfDistinctValues(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] answer = new int[m][n];

        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                int leftX = i;
                int leftY = j;
                int rightX = i;
                int rightY = j;
                Set<Integer> leftSet = new HashSet();
                Set<Integer> rightSet = new HashSet();

                while(leftX > 0 && leftY > 0) {
                    --leftX;
                    --leftY;
                    leftSet.add(grid[leftX][leftY]);
                }

                while(rightX < m - 1 && rightY < n - 1) {
                    ++rightX;
                    ++rightY;
                    rightSet.add(grid[rightX][rightY]);
                }

                answer[i][j] = Math.abs(leftSet.size() - rightSet.size());
            }
        }

        return answer;
    }
}
